from app.models.sys_manage.user import User
from app.utils.mysql_util import MySQLTool
import hashlib

def update_password_hashes():
    """
    更新现有用户的密码为哈希值
    """
    mysql_tool = MySQLTool.get_instance()
    users = mysql_tool.query_data(User)
    
    for user in users:
        # 获取原始密码
        original_password = user.password
        # 计算SHA256哈希
        hashed_password = hashlib.sha256(original_password.encode()).hexdigest()
        
        # 更新用户密码
        user_filter = User(user_id=user.user_id)
        user_update = User(password=hashed_password)
        mysql_tool.update_data(User, user_filter, user_update) 